package vip.xiaonuo.feed.modular.task;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.timer.CommonTimerTaskRunner;
import vip.xiaonuo.dev.modular.config.entity.DevConfig;
import vip.xiaonuo.dev.modular.config.mapper.DevConfigMapper;
import vip.xiaonuo.dev.modular.message.param.DevMessageSendParam;
import vip.xiaonuo.dev.modular.message.service.DevMessageService;
import vip.xiaonuo.feed.modular.feedstocks.entity.FeedStocksView;
import vip.xiaonuo.feed.modular.feedstocks.mapper.FeedStocksViewMapper;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2023/12/28、12:10
 */
@Slf4j
@Component
public class FeedStockWarnTask implements CommonTimerTaskRunner {
    @Resource
    private FeedStocksViewMapper feedStocksViewMapper;

    @Resource
    private DevConfigMapper devConfigMapper;

    @Resource
    private DevMessageService devMessageService;

    private final String CONFIG_KEY = "biz_feedstock_thresholds";

    /**
     * 库存预警的执行方法
     * 逻辑如下：
     *      首先查询所有库存记录，如果发现低于阈值则抛出异常，并发送站内信息/邮件
     */
    @Override
    public void action() {
        List<FeedStocksView> feedStocks = feedStocksViewMapper.selectList(null);
        int threshold = getThreshold();
        if (ObjectUtil.isNotEmpty(feedStocks)){
            for (FeedStocksView feedStock : feedStocks) {
                BigDecimal quantity = feedStock.getQuantity();
                BigDecimal bigDecimalThreshold = BigDecimal.valueOf(threshold);
                if (quantity.compareTo(bigDecimalThreshold) < 0){
                    sendMessage(feedStock);
                    log.info("库存预警执行：{}",feedStock.getTypeName());
                    throw new CommonException(feedStock.getTypeName() + "库存不足！请及时处理");
                }
            }
        }

    }

    /**
     * 获取阈值
     *
     * @return {@link Integer}
     */
    private Integer getThreshold(){
        LambdaQueryWrapper<DevConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DevConfig::getConfigValue).eq(DevConfig::getConfigKey,CONFIG_KEY);
        DevConfig devConfig = devConfigMapper.selectOne(queryWrapper);
        String configValue = devConfig.getConfigValue();
        return Integer.valueOf(configValue);
    }


    /**
     * 发送站内消息
     *
     * @param feedStock 饲料库存
     */
    private void sendMessage(FeedStocksView feedStock){
        List<String> idList = new ArrayList<>();
        idList.add("1543837863788879871");
        DevMessageSendParam devMessageSendParam = new DevMessageSendParam();
        devMessageSendParam.setCategory("BIZ");
        devMessageSendParam.setContent(feedStock.getTypeName() + "库存不足！请及时处理");
        devMessageSendParam.setSubject("库存预警信息");
        devMessageSendParam.setReceiverIdList(idList);
        devMessageService.send(devMessageSendParam);
    }

}
